//
//  SwordDetailViewController.m
//  Tiger
//
//  Created by  on 12-8-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SwordDetailViewController.h"
#import "ASIHTTPRequest.h"
#import "asyncimageview.h"
@interface SwordDetailViewController ()

@end

@implementation SwordDetailViewController
@synthesize m_CatgID;
@synthesize m_KnowTitle;
-(id) init
{
    if (self = [super init]) {
        
    }
    return self;
}
-(void) dealloc
{
    [p_URLRequest setDelegate:nil];
    [p_URLRequest release];
    [p_ScrollContent release];
    [m_KnowTitle release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIImageView*  tBGImgView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"screen_bg.png"]];
    tBGImgView.frame =CGRectMake(0, 0, 320, 480-88);
    [self.view addSubview:tBGImgView];
    [tBGImgView release];
    
    p_ScrollContent = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-88)];
    [self.view addSubview:p_ScrollContent];
    
    UILabel* tTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
    [tTitleLab setText:m_KnowTitle];
    [tTitleLab setFont:[UIFont systemFontOfSize:16]];
    [tTitleLab setBackgroundColor:[UIColor clearColor]];
    [tTitleLab setTextColor:[UIColor whiteColor]];
    
    [p_ScrollContent addSubview:tTitleLab];
    
    NSString* tURLString = [NSString stringWithFormat:@"http://www.speedtiger.com.cn/ios3/knows_show.html?cateID=%d",m_CatgID];
    p_URLRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:tURLString]];
    [p_URLRequest setDelegate:self];
    [p_URLRequest startAsynchronous];
    [p_URLRequest retain];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark-
#pragma mark ASIHTTPRequestDelegate
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@",[request responseString]);
    NSData *responseData = [request responseData];
	NSString *responseStr = [[[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding] autorelease];
    
    NSDictionary* tEnumDicData = [responseStr JSONValue];
    
    int moveH = 40;
    NSArray* tItems = [tEnumDicData objectForKey:@"items"];
    for (NSDictionary* tDic in tItems) {
        NSString* tOneImg = [tDic objectForKey:@"picUrl"];
        if ([tOneImg length] > 0) {
            AsyncImageView* tAsImg =[[AsyncImageView alloc] initWithFrame:CGRectMake(60, moveH, 200, 150)];
            tAsImg._numStateRequested = need_Request;
            tAsImg._imageRequestURL = tOneImg;
            tAsImg.m_BoolSetImage = YES;
            [p_ScrollContent addSubview:tAsImg];
            [tAsImg customLoadImageFromURL];
            moveH+= 165;
        }
        NSString* tContentStr = [tDic objectForKey:@"knowContent"];
        
        UITextView* tContentLab = [[UITextView alloc] initWithFrame:CGRectMake(20, moveH, 280, 100)];
        [tContentLab setFont:[UIFont systemFontOfSize:14]];
        [tContentLab setBackgroundColor:[UIColor clearColor]];
        [tContentLab setTextColor:[UIColor lightGrayColor]];
        [tContentLab setText:tContentStr];

        [p_ScrollContent addSubview:tContentLab];
        CGSize aTextSize = tContentLab.contentSize;
        tContentLab.frame =CGRectMake(20, moveH, 280, aTextSize.height);
        tContentLab.scrollEnabled =NO;
        [tContentLab release];
        moveH+= aTextSize.height+15;
    }
    
    [p_ScrollContent setContentSize:CGSizeMake(320, moveH)];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"无可用的网络连接" delegate:self 
										   cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
	[alert addButtonWithTitle:@"确定"];
	[alert show];
}

@end
